package com.example.administrator.animationdemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.administrator.animationdemo.view.TipCircleView;
import com.example.administrator.animationdemo.view.TipCircleViewGroup;

import java.lang.ref.WeakReference;


public class MainActivity extends Activity {

    private TipCircleViewGroup viewGroup;
    private int inputFlag;
    private int stepSum; // 往一个方向的总偏移距离
    private int step; // 每一帧的偏移距离
    private int moveCount; // 总移动回合数, 从向左偏移动开始到从右边返回到起始位置此为一个回合
    private int perDirectionCount; // 每回合方向移动次数
    private SecurityHandle handle;
    private long sleepMillis;
    private boolean[] directions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewGroup = (TipCircleViewGroup) findViewById(R.id.TipCircleViewGroup);
        Button startBtn = (Button) findViewById(R.id.startBtn);
        Button inputBtn = (Button) findViewById(R.id.inputBtn);
        Button resetBtn = (Button) findViewById(R.id.resetBtn);

        startBtn.setOnClickListener(new StartBtnListener());
        inputBtn.setOnClickListener(new InputBtnListener());
        resetBtn.setOnClickListener(new ResetBtnListener());

        init();
    }

    private void init() {
        inputFlag = -1;
        stepSum = 100; // 往一个方向的总偏移距离
        step = 10; // 每一帧偏移像素
        moveCount = 5; // 回合
        perDirectionCount = 4; // 每回合方向移动次数
        sleepMillis = 5; // 每一帧时间（毫秒）
        directions = new boolean[stepSum / step * perDirectionCount * moveCount];
        handle = new SecurityHandle(this);
        initDirections();
        initViewGroup();
    }

    private void initDirections() {
        int index = 0;
        boolean directionL;
        for (int i = 0; i < moveCount; i++) {
            for (int j = 0; j < perDirectionCount; j++) {
                directionL = j == 0 || j == 3;
                for (int k = 0; k < stepSum / step; k++) {
                    directions[index++] = directionL;
                }
            }
        }
    }

    private void initViewGroup() {
        for (int i = 0; i < 6; i++) {
            TipCircleView tipCircleView = new TipCircleView(this);
            viewGroup.addView(tipCircleView);
        }
    }

    private void shakeCircle() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (boolean direction : directions) {
                    Message message = new Message();
                    message.what = 1;
                    message.obj = direction;
                    handle.sendMessage(message);
                    try {
                        Thread.sleep(sleepMillis);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    private class StartBtnListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            shakeCircle();
        }
    }

    private class InputBtnListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            inputFlag += 1;
            if (inputFlag == 5) {
                // 执行动画并reset
                shakeCircle();
                inputFlag = -1;
                viewGroup.reset();
            } else {
                viewGroup.fillCircles(inputFlag);
            }
        }
    }

    private class ResetBtnListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            inputFlag = -1;
            viewGroup.reset();
        }
    }

    public static class SecurityHandle extends Handler {
        private WeakReference<MainActivity> outer;

        public SecurityHandle(MainActivity mainActivity) {
            outer = new WeakReference<>(mainActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            if (outer != null) {
                if (msg.what == 1) {
                    MainActivity out = outer.get();
                    out.viewGroup.moveLayout((boolean) msg.obj, out.step);
                }
            }
        }
    }

}
